﻿using FluentNHibernate.Mapping;
using FluentNHibernate.Conventions.Helpers;

namespace NMA.Infrastructure.NHibernate.Mapping
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;

    /// <summary>
    /// http://wiki.fluentnhibernate.org/Conventions
    /// http://wiki.fluentnhibernate.org/Converting_to_new_style_conventions
    /// http://wiki.fluentnhibernate.org/Available_conventions
    /// http://www.sullivansoftdev.com/blog/tag/fluent-nhibernate/
    /// http://marekblotny.blogspot.com/2008/12/fluent-nhibernate-conventions-and.html
    /// http://www.zachariahyoung.com/zy/category/fluent-nhibernate.aspx
    /// http://marekblotny.blogspot.com/2009/02/fluent-nhbernate-and-collections.html
    /// http://www.codeproject.com/KB/database/Nhibernate_Made_Simple.aspx
    /// </summary>
    public class NewsMap : ClassMap<News>
    {
        public NewsMap()
        {
            LazyLoad();

            Table("[NMA].[dbo].[News]");

            Id(x => x.Id, "ID").GeneratedBy.Guid();
            Map(x => x.Title, "Title").Not.Nullable().Length(500);
            Map(x => x.ShortDescription, "ShortDescription").Not.Nullable();
            Map(x => x.Content, "Content").Not.Nullable();
            Map(x => x.IsPublish, "IsPublish").Not.Nullable();
            Map(x => x.PublishedDate, "PublishedDate");
            Map(x => x.PublishedPerson, "PublishedPerson").Length(30);
            Map(x => x.NumberOfView, "NumberOfView");
            Map(x => x.Image, "Image");
            Map(x => x.CreatedDate, "CreatedDate").Not.Nullable();
            Map(x => x.UpdatedDate, "UpdatedDate");
            Map(x => x.CreatedUser, "CreatedUser").Not.Nullable().Length(30);
            Map(x => x.UpdatedUser, "UpdatedUser").Length(30);
            References<Category>(x => x.Category)
                .Column("Category_Id")
                .Fetch.Join();                
            HasOne<Poll>(x => (Poll)x.Poll)
                //.Constrained()        // affects the order in which save and update are cascaded
                .Cascade.All()
                .Fetch.Join();
        }
    }
}
